import fetch from 'common/src/utils/fetch';
import UTILS from "common/src/utils/utils";
/**
 * 获取补缓考申请的学生列表信息
 * @param {*} data
 */
function delayExamStudentList (data) {
    return fetch({
        url: '/api/scoremanagementservice/delayExam/delayExamStudentList',
        method: 'POST',
        data
    });
}
/**
 * 提交暂存
 * @param {*} data
 */
function saveDelayExamScores(data = {}) {
    return fetch({
        url: '/api/scoremanagementservice/delayExam/saveDelayExamScores',
        method: 'post',
        data
    });
}

/**
 * 提交发布
 * @param {*} data
 */
function publishDelayExamScores(data = {}) {
    return fetch({
        url: '/api/scoremanagementservice/delayExam/publishDelayExamScores',
        method: 'post',
        data
    });
}

/**
 * 导出接口
 * @param {*} data
 */
function exportDelayData(params = {}, selectIds = '') {
    let url = '/api/scoremanagementservice/delayExam/delayExamListExport';
    let string = '';
    for (var key in params) {
        if (params[key] === '') {
            continue;
        }
        string += `${key}=${params[key]}&`;
    }
    let restApi = `${url}?${string.substring(0, string.length - 1)}`;
    return fetch({
        url: restApi,
        method: 'post',
        headers: { 'Content-Type': 'text/plain;charset=UTF-8' },
        responseType: 'blob',
        data: selectIds
    });
}

function getScoreRegisterInfo(data) {
    return fetch({
        url: '/api/scoremanagementservice/delayExam/getScoreRegisterInfo',
        method: 'post',
        data
    });
}

function exportScorePdf(data) {
    return fetch({
        url: '/api/scoremanagementservice/delayExam/exportScorePdf',
        method: 'post',
        responseType: 'blob',
        data: data
    }).then(res => {
        UTILS.downloadFile(res, '补缓考成绩登记表.pdf', 'application/vnd.ms-pdf;charset=utf-8');
    });
}

function exportGeneralTable(idList) {
    return fetch({
        url: '/api/scoremanagementservice/delayExam/batchExportScore?language=cn',
        method: 'post',
        responseType: 'blob',
        data: idList
    }).then(res => {
        if(idList.length > 1){
            UTILS.downloadFile(res, '补缓考成绩表.zip', 'application/zip;charset=utf-8');
        }else{
            UTILS.downloadFile(res, `补缓考成绩表${idList[0].id}.pdf`, 'application/vnd.ms-pdf;charset=utf-8');
        }
    })
}

const api = {
    delayExamStudentList: delayExamStudentList,
    saveDelayExamScores: saveDelayExamScores,
    publishDelayExamScores: publishDelayExamScores,
    exportDelayData: exportDelayData,
    getScoreRegisterInfo: getScoreRegisterInfo,
    exportScorePdf: exportScorePdf,
    exportGeneralTable: exportGeneralTable
};

export default api;
